{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we test and check the performance of the array_arange implementation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "from functools import partial\n",
    "from itertools import izip as zip, imap as map\n",
    "from numpy import arange\n",
    "import numpy as np\n",
    "\n",
    "onesi = partial(np.ones, dtype=np.int32)\n",
    "cumsumi = partial(np.cumsum, dtype=np.int32)\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng(start, end):\n",
    "    return np.hstack(map(arange, start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_for_list(start, end):\n",
    "    return np.hstack([arange(s, e) for s, e in zip(start, end)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_for_gen(start, end):\n",
    "    return np.hstack((arange(s, e) for s, e in zip(start, end)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_concat(start, end):\n",
    "    return np.concatenate(list(map(arange, start, end)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_concat_list(start, end):\n",
    "    return np.concatenate([arange(s, e) for s, e in zip(start, end)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_concat_tuple(start, end):\n",
    "    return np.concatenate(tuple(map(arange, start, end)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def aarng_one(start, end):\n",
    "    n = end - start\n",
    "    arr = onesi(n.sum())\n",
    "    # set pointers correct for cumsum\n",
    "    ptr = cumsumi(n[:-1])\n",
    "    arr[0] = start[0]\n",
    "    arr[ptr] = start[1:]\n",
    "    # Correct for previous values\n",
    "    arr[ptr] -= start[:-1] + n[:-1] - 1\n",
    "    return cumsumi(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Samples\n",
    "N = 200000\n",
    "start = np.random.randint(1000, size=N)\n",
    "end = start + np.random.randint(10, size=N) + start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array((aarng(start, end)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_for_list(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_for_gen(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_concat(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_concat_list(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_concat_tuple(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit np.array(aarng_one(start, end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
